package com.leoblog.test.dao;

import java.util.Date;
import java.util.List;

import com.leoblog.dao.impl.UserDao;
import com.leoblog.domain.User;
import com.leoblog.domain.WebSite;
import com.leoblog.test.base.AbstractUTestBaseSpringDaoTestCase;

public class UTestUserDao extends AbstractUTestBaseSpringDaoTestCase
{

	private UserDao userDao;

	public void setUserDao(UserDao userDao)
	{
		this.userDao = userDao;
	}

	public void testInsert()
	{

		User userDOM = new User();

		userDOM.setCreateDate(new Date());
		userDOM.setEmail("liangjinhua@gmail.com");
		userDOM.setMsn("liangjinhua02@hotmail.com");
		userDOM.setNickName("leo");
		userDOM.setPassword("111");
		userDOM.setQq("1234532");
		userDOM.setStatus("1");
		userDOM.setUserName("leoseu");

		userDao.save(userDOM);

		User loadDOM = userDao.get(userDOM.getId());

		assertEquals(userDOM.getId(), loadDOM.getId());
		assertEquals(userDOM.getCreateDate(), loadDOM.getCreateDate());
		assertEquals(userDOM.getEmail(), loadDOM.getEmail());
		assertEquals(userDOM.getMsn(), loadDOM.getMsn());
		assertEquals(userDOM.getNickName(), loadDOM.getNickName());
		assertEquals(userDOM.getPassword(), loadDOM.getPassword());
		assertEquals(userDOM.getQq(), loadDOM.getQq());
		assertEquals(userDOM.getStatus(), loadDOM.getStatus());
		assertEquals(userDOM.getUserName(), loadDOM.getUserName());
	}

	@SuppressWarnings("unchecked")
	public void testWebSiteInsert()
	{
		WebSite website = new WebSite();
		website.setCreateDate(new Date());
		website.setDescription("abc");
		website.setHandle("leo");
		website.setIgnoreWords("sex");
		website.setModifiedDate(new Date());
		website.setName("Leo");
		website.setStatus("1");
		website.setTheme("111");
		
		User userDOM = new User();

		userDOM.setCreateDate(new Date());
		userDOM.setEmail("liangjinhua@gmail.com");
		userDOM.setMsn("liangjinhua02@hotmail.com");
		userDOM.setNickName("leo");
		userDOM.setPassword("111");
		userDOM.setQq("1234532");
		userDOM.setStatus("1");
		userDOM.setUserName("leoseu");
		
		website.setUser(userDOM);
		userDOM.getWebsites().add(website);
		userDao.save(userDOM);
		
		
		User loadDOM = userDao.get(userDOM.getId());

		assertEquals(userDOM.getId(), loadDOM.getId());
		assertEquals(userDOM.getCreateDate(), loadDOM.getCreateDate());
		assertEquals(userDOM.getEmail(), loadDOM.getEmail());
		assertEquals(userDOM.getMsn(), loadDOM.getMsn());
		assertEquals(userDOM.getNickName(), loadDOM.getNickName());
		assertEquals(userDOM.getPassword(), loadDOM.getPassword());
		assertEquals(userDOM.getQq(), loadDOM.getQq());
		assertEquals(userDOM.getStatus(), loadDOM.getStatus());
		assertEquals(userDOM.getUserName(), loadDOM.getUserName());
		assertEquals(1, loadDOM.getWebsites().size());
		
		List loadWebSite = loadDOM.getWebsites();
		
		assertEquals(website.getDescription(), ((WebSite)loadWebSite.get(0)).getDescription());
		assertEquals(website.getHandle(), ((WebSite)loadWebSite.get(0)).getHandle());
		assertEquals(website.getIgnoreWords(), ((WebSite)loadWebSite.get(0)).getIgnoreWords());
		assertEquals(website.getName(), ((WebSite)loadWebSite.get(0)).getName());
		assertEquals(website.getStatus(), ((WebSite)loadWebSite.get(0)).getStatus());
		assertEquals(website.getTheme(), ((WebSite)loadWebSite.get(0)).getTheme());	
		assertEquals(loadDOM.getId(), ((WebSite)loadWebSite.get(0)).getUser().getId());	
	}
}
